/**
 * Copyright &copy; 2011-2020 lnint Inc. All rights reserved.
 * 
 * 修改信息：v1.0 doc做成
 * A: 新增类  王朋飞  2015-03-11 15:20:21
 */
package com.lnint.jess.core.workflow.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lnint.jess.common.persistence.Page;
import com.lnint.jess.common.web.BaseController;
import com.lnint.jess.core.workflow.service.ModelService;

/**
 * <P>流程模型控制器</P>
 * 
 * @version 1.0
 * @author 王朋飞 2015-03-12 00:00:00
 * @since JDK 1.6
 */
@Controller
@RequestMapping(value = "${adminPath}/workflow/model")
public class ModelController extends BaseController {

  protected Logger logger = LoggerFactory.getLogger(getClass());

  @Autowired
  RepositoryService repositoryService;
  
  @Autowired
  private ModelService modelService;
  
  	/**
	 * 模型列表
	 */
  @RequiresPermissions("workflow:model:view")
  @RequestMapping(value = { "list", "" })
  public String modelList(String category, HttpServletRequest request, HttpServletResponse response, Model model) {
	  Page<org.activiti.engine.repository.Model> page = modelService.modelList(
				new Page<org.activiti.engine.repository.Model>(request, response), category);

	  model.addAttribute("page", page);
	  model.addAttribute("category", category);

	  return "workflow/model_list";
  }
  
  	/**
	 * 创建模型
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String create(Model model) {
		return "workflow/model_form";
	}
	
	/**
	 * 创建模型
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public void create(String name, String key, String description, String category,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			org.activiti.engine.repository.Model modelData = modelService.create(name, key, description, category);
			response.sendRedirect(request.getContextPath() + "/modeler/service/editor?id=" + modelData.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("创建模型失败：", e);
		}
	}
	
	/**
	 * 根据Model部署流程
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "deploy")
	public String deploy(String id, RedirectAttributes redirectAttributes) {
		String message = modelService.deploy(id);
		redirectAttributes.addFlashAttribute("message", message);
		return "redirect:" + adminPath + "/workflow/model/list";
	}
	
	/**
	 * 导出model的xml文件
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "export")
	public void export(String id, HttpServletResponse response) {
		modelService.export(id, response);
	}

	/**
	 * 更新Model分类
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "updateCategory")
	public String updateCategory(String id, String category, RedirectAttributes redirectAttributes) {
		modelService.updateCategory(id, category);
		redirectAttributes.addFlashAttribute("message", "设置成功，模块ID=" + id);
		return "redirect:" + adminPath + "/workflow/model";
	}
	
	/**
	 * 删除Model
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("workflow:model:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		modelService.delete(id);
		redirectAttributes.addFlashAttribute("message", "删除成功，模型ID=" + id);
		return "redirect:" + adminPath + "/workflow/model";
	}

}
